<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('front.index');
    }
    public function people()
    {
        return view('front.people');
    }
    public function list()
    {

        return view('front.people_list');
    }
    public function list_adm()
    {
        $oUser = User::paginate(5);
        return view('admin.list')->with('oUser', $oUser);
    }
    public function login()
    {
        return view('admin.login');
    }
    public function dologin(Request $request){
        $name=$request->name;
        $password=$request->password;
        if (Auth::attempt(['name'=>$name,'password'=>$password])){
            return json_encode(['success'=>true,'msg'=>"用户成功登录"]);
        }
        else{
            return json_encode(['success'=>false,'msg'=>'用户名和密码错误，请重新登录']);
        }
    }
    public function logout(){
        Auth::logout();
        return redirect('/login');
    }





    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(){
        return view('admin.create-user');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        $datas = $request->all();
        $rules = [
            'name' => 'required|alpha_dash|min:3|unique:users',
            'password' => 'required|min:6|confirmed'
        ];
        $message = [
            'name.required' => '用户名不能为空',
            'name.alpha_dash' => '用户名必须是字母、数字，短破折号（-）和下划线（_）',
            'name.min' => '用户名不得少于:min字符',
            'name.unique' => '用户名已存在，不能重复',
            'password.required' => '密码不能为空',
            'password.min' => '密码不能少于:min字符',
            'password.confirmed' => '两次密码输入不一致'
        ];
        $validate = Validator::make($datas, $rules, $message);
        if ($validate->fails()) {
            $msg = $validate->messages()->first();
            return json_encode(['success' => false, 'msg' => $msg]);
        } else {
            $datas['password'] = Hash::make($request->password);
            User::create($datas);
            return json_encode(['success' => true, 'msg' => '成功提交']);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $oUser = User::find($id);
        if ($oUser == null) {
            abort(404);
        } else {
            return view('admin.edit-user')->with('oUser', $oUser);
        }
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        $ouser = User::find($id);
        if ($ouser == null) {
            abort(404);
        }
        $datas = $request->all();
        $rules = [
            'password' => 'required|min:6|confirmed'
        ];
        $message = [
            'password.required' => '密码不能为空',
            'password.min' => '密码不能少于:min字符',
            'password.confirmed' => '两次密码输入不一致'
        ];
        $validate = Validator::make($datas, $rules, $message);
        if ($validate->fails()) {
            $msg = $validate->messages()->first();
            return json_encode(['success' => false, 'msg' => $msg]);
        } else {
            $ouser->password = Hash::make($datas['password']);
            $ouser->save();
            return json_encode(['success' => true, 'msg' => '成功提交']);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $user = User::find($id);
        $user->delete();
        return json_encode(['msg'=>'删除成功']);
    }

}
